package com.mingtu.guards.view;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.mingtu.common.utils.CalendarPickerUtils;
import com.mingtu.common.utils.UIUtils;
import com.mingtu.guards.R;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @ClassName :
 * @Author : wkk
 * @Date : 2023-10-9 10:01:39
 * @Email : 1281573890@qq.com
 * @Eescription :
 */
public class TimeFilterView extends FrameLayout {
    @BindView(R.id.tv_start_time)
    TextView tvStartTime;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.layout_time)
    LinearLayout layoutTime;
    private View view;
    private Unbinder unbinder;
    private Activity activity;
    private Context context;
    private OnTimeResultListener onTimeResultListener = null;

    public TimeFilterView(@NonNull Context context) {
        this(context, (AttributeSet) null, 0);
    }

    public TimeFilterView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TimeFilterView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        view = LayoutInflater.from(context).inflate(R.layout.layout_time_filter, this, true);
        this.activity = (Activity) context;
        this.context = context;
        unbinder = ButterKnife.bind(this, view);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        unbinder.unbind();
    }

    @OnClick({R.id.layout_time})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_time:
                CalendarPickerUtils.getInstance().showPicker(activity, context);
                CalendarPickerUtils.getInstance().setOnSureClickListener(new CalendarPickerUtils.OnSureClickListener() {
                    @Override
                    public void onRusult(Date startDate, Date endDate) {
                        String uiBeginTime = UIUtils.getTime(startDate);
                        String uiendTime = UIUtils.getTime(endDate);
                        tvStartTime.setText(uiBeginTime);
                        tvEndTime.setText(uiendTime);

                        onTimeResultListener.onResult(startDate, endDate);
                    }
                });
                break;
        }
    }

    public void setOnTimeResultListener(OnTimeResultListener onTimeResultListener) {
        this.onTimeResultListener = onTimeResultListener;
    }

    public static interface OnTimeResultListener {
        void onResult(Date startDate, Date endDate);
    }

    public void resetData() {
        tvStartTime.setText("");
        tvEndTime.setText("");
    }

    public void setStartTime(String startTime) {
        tvStartTime.setText(startTime);
    }

    public void setEndTime(String endTime) {
        tvEndTime.setText(endTime);
    }
}
